'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-04-09 19:25:23
'''
import os
import sys
from subprocess import *
import random, time, json, sys
class galaxy_pred():
    def __init__(self, model_name, batch_size, shuffle_size, watch_num, neg_rate, ps_url, process_num, dates, f_path, run_shell, net_conf, labels, random_sleep=0):
        self.model_name = model_name
        self.batch_size = batch_size
        self.process_num = process_num
        self.shuffle_size = shuffle_size
        self.watch_num = watch_num
        self.neg_rate = neg_rate
        self.ps_url = ps_url
        self.run_shell = run_shell
        self.dates = dates
        self.f_path = f_path
        self.random_sleep = random_sleep
        self.net_conf = net_conf
        self.labels = labels
        self.shell_p = self.run_shell + " -layer /data1/xuejian/sync/galaxy/conf/" + self.net_conf + " -mn " + self.model_name + " -ps_url " + self.ps_url + ':' + str(sys.argv[1]) + " -w " + str(self.watch_num) + " -s " + str(self.shuffle_size) + " -b " + str(self.batch_size) + " -ne " + str(self.neg_rate) + " -labels " + self.labels
        # print('shell = ', self.shell_p)

    def pred(self):
        proc = Popen(self.shell_p, shell=True, stdin=PIPE)
        # print('process start')
        for date in self.dates:
            # print(date)
            with open(self.f_path + date, 'rb') as f_date:
                # print(self.f_path + date)
                line = f_date.readline()
                while line != b'':
                    if line == b'':
                        break
                    proc.stdin.write(line)
                    proc.stdin.flush()
                    line = f_date.readline()
        
        proc.stdin.close()
        proc.wait()



if __name__ == '__main__':
    dates = []
    dates.extend(['2020-04-08'])
    # dates.extend(['2020-03-0' + str(i) for i in range(1, 10)])
    # dates.extend(['2020-03-' + str(i) for i in range(10, 31)])
    # dates.extend(['2020-04-0' + str(i) for i in range(1, 8)])
    # dates.extend(["2020-01-" + str(i) for i in range(15, 16)])
    cut_path = '/data3/training_data/galaxy_data_cut/'
    single_path = '/data3/training_data/galaxy_data_new/'
    nums = 1
    if nums <= 1:
        real_path = single_path
        watch_num = 200000
    else:
        real_path = cut_path
        watch_num = 50000
    ps_url = '172.21.39.3' #offlineps-03
    net_name = 'success_deepfm.net'
    preds = galaxy_pred('success_fm', 200, 10000, watch_num, 0.99, ps_url, nums, dates, real_path, '/data1/xuejian/sync/galaxy/galaxy_pred_score', net_name, 'one', 15)
    preds.pred()